import 'package:flutter/material.dart';
import 'package:wallpaper_app/components/AlertDialog/my_loading.dart';
import 'package:wallpaper_app/components/Images/build_item.dart';
import 'package:wallpaper_app/components/empty.dart';
import 'package:wallpaper_app/model/options_base.dart';
import 'package:waterfall_flow/waterfall_flow.dart';

class ImageListBase extends StatefulWidget {
  final List imageList;
  final bool isLoading;
  final double? top;
  final bool isWaterfallFlow;
  const ImageListBase({
    super.key,
    required this.imageList,
    required this.isLoading,
    this.top,
    this.isWaterfallFlow = false,
  });

  @override
  State<ImageListBase> createState() => _ImageListState();
}

class _ImageListState extends State<ImageListBase> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.all(5),
      child: _buildShowBody(),
    );
  }

  Widget _buildShowBody() {
    final theme = Theme.of(context).colorScheme;
    if (widget.isLoading && widget.imageList.isEmpty) {
      return const Center(
        child: CircularProgressIndicator(),
      );
    } else if (widget.imageList.isEmpty) {
      return Empty();
    } else {
      return CustomScrollView(
        physics: const AlwaysScrollableScrollPhysics(),
        cacheExtent: 500,
        slivers: [
          if (widget.top != null)
            SliverToBoxAdapter(
              child: SizedBox(
                height: widget.top!,
              ),
            ),
          SliverPadding(
            padding: const EdgeInsets.only(top: 5),
            // 瀑布流核心组件：SliverWaterfallFlow
            sliver: SliverWaterfallFlow(
              // 网格代理：控制列数、间距、动态宽高比
              gridDelegate: SliverWaterfallFlowDelegateWithFixedCrossAxisCount(
                crossAxisCount: OptionsBase().horizontalColumns(context),
                crossAxisSpacing: 5,
                mainAxisSpacing: 5,
              ),
              // 子项代理
              delegate: SliverChildBuilderDelegate(
                (context, index) {
                  final image = widget.imageList[index];
                  // 计算宽高比
                  double aspectRatio = 1.7;
                  if (widget.isWaterfallFlow &&
                      image.width != null &&
                      image.height != null) {
                    aspectRatio = image.width! / image.height!;
                  }
                  return AspectRatio(
                    aspectRatio: aspectRatio,
                    child: BuildItem(
                      key: ValueKey(image.smallPath),
                      imgs: widget.imageList,
                      index: index,
                    ),
                  );
                },
                childCount: widget.imageList.length, // 子项数量（图片列表长度）
                addAutomaticKeepAlives: true,
                addRepaintBoundaries: true,
              ),
            ),
          ),
          SliverToBoxAdapter(
            child: Padding(
              padding: const EdgeInsets.all(3.0),
              child: SizedBox(
                height: 30,
                child: Center(
                  child: widget.isLoading
                      ? MyLoading(
                          type: 1,
                          size: 30,
                        )
                      : Text(
                          '~ 已经到底了 ~',
                          style: TextStyle(
                            color: theme
                                .onSurface
                                .withAlpha(90),
                          ),
                        ),
                ),
              ),
            ),
          )
        ],
      );
    }
  }
}
